package com.inforim.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import com.cognos.developer.schemas.bibus._3.BaseClass;
import com.cognos.developer.schemas.bibus._3.ContentManagerServiceStub;
import com.cognos.developer.schemas.bibus._3.Directory;
import com.cognos.developer.schemas.bibus._3.Group;
import com.cognos.developer.schemas.bibus._3.Policy;
import com.cognos.developer.schemas.bibus._3.PropEnum;
import com.cognos.developer.schemas.bibus._3.QueryOptions;
import com.cognos.developer.schemas.bibus._3.Role;
import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
import com.cognos.developer.schemas.bibus._3.Sort;
import com.inforim.cognos.CognosClient;
import com.inforim.cognos.utils.CognosHelper;
import com.inforim.module.CSVObject;
import com.inforim.module.CogAccount;
import com.inforim.module.CogObject;
import com.inforim.module.CogRole;

public class OutputAuthMember {
	public static void main(String[] args) {
//		CognosClient cognosClient = CognosHelper.getClient();
//		cognosClient.setDispatch("http://shasx011:9300/p2pd/servlet/dispatch");
//		cognosClient.setUsername("oax62349");
//		cognosClient.setPassword("etk6JaV6");
//		cognosClient.setNamespace("SED");
		String dispatchUrl="";
		String userName="";
		String password="";
		String nameSpace="";
		String path="";
		for(int i=0;i<args.length;i++){
			String param=args[i];
			if(param.startsWith("-dispatchUrl=")){
				dispatchUrl=param.replaceAll("-dispatchUrl=", "");
			}
			if(param.startsWith("-userName=")){
				userName=param.replaceAll("-userName=", "");
			}
			if(param.startsWith("-password=")){
				password=param.replaceAll("-password=", "");
				try {
					DESUtil desUtil = new DESUtil();
					password = desUtil.decrypt(password);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(param.startsWith("-nameSpace=")){
				nameSpace=param.replaceAll("-nameSpace=", "");
			}
			if(param.startsWith("-path=")){
				path=param.replaceAll("-path=", "");
			}
		}
		
		//login
		CognosClient cognosClient = CognosHelper.getClient();
		cognosClient.setDispatch(dispatchUrl);
		cognosClient.setUsername(userName);
		cognosClient.setPassword(password);
		cognosClient.setNamespace(nameSpace);
		try {
			cognosClient.logon();
			// System.out.println("logon success!");
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out
					.println("Connect to Cognos failed, please check username & pasword in config.xml or Cognos enviroment.");
			return;
		}
		
		CSVAuthMember cam=new CSVAuthMember();
		BaseClass[] groups = cam.getChildrenAsType(cognosClient, "CAMID(\":\")", "group");
		BaseClass[] roles = cam.getChildrenAsType(cognosClient, "CAMID(\":\")", "role");
		String output = "\"COGNOSROLE\",\""+nameSpace+"ROLE\",\"NAME\",\"MUDID\"\r\n";
		for(int i=0;i<groups.length;i++){
			Group group=(Group)groups[i];
			String groupSearchPath=group.getSearchPath().getValue();
			output+=cam.getSecurityObject(cognosClient, groupSearchPath);
		}
		for(int i=0;i<roles.length;i++){
			Role role=(Role)roles[i];
			String roleSearchPath=role.getSearchPath().getValue();
			output+=cam.getSecurityObject(cognosClient, roleSearchPath);
		}
		File file = new File(path+File.separator+"authList.csv");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF});
			fos.write(output.getBytes("UTF-8"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fos.close();
			System.out.println(file.getAbsolutePath()+" has created!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
